package com.xj.opcua.data.template;

import javax.annotation.Resource;

import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import com.xj.opcua.data.entity.OpcUaSubscriptionData;

@Component
public class OpcUaSubscriptionDataTemplate {
	
	@Resource
    MongoTemplate mongotemplate;

    public OpcUaSubscriptionData save(OpcUaSubscriptionData data){
    	return mongotemplate.insert(data);
    }
    
    public OpcUaSubscriptionData getByUrl(String url){
    	try {
    		Query query = new Query(Criteria.where("url").is(url));
        	query.with(Sort.by(Direction.DESC, "createTime"));
        	OpcUaSubscriptionData list = mongotemplate.findOne(query, OpcUaSubscriptionData.class);
            return list;
    	}catch (Exception e) {
			return null;
		}
    	
    }
    
    public OpcUaSubscriptionData updateSubscriptionData(OpcUaSubscriptionData data){
    	Query query = new Query(Criteria.where("id").is(data.getId()));
		Update update = new Update();
        update.set("interval", data.getInterval());
		mongotemplate.updateFirst(query,update,OpcUaSubscriptionData.class);
		return  data;
    }
   
}

